#!/bin/bash

millisecond=1
second=$(( 1000 * millisecond ))
minute=$(( 60 * second ))

gather_logging_resources() {
  set +e
  local LOGGING_NS=$1
  local outdir=${2:-$ARTIFACT_DIR}
  oc -n ${LOGGING_NS} get configmaps -o yaml > $outdir/configmaps.yaml 2>&1 || :
  oc -n ${LOGGING_NS} get secrets -o yaml > $outdir/secrets.yaml 2>&1 || :
  oc -n ${LOGGING_NS} get cronjobs -o yaml > $outdir/cronjobs.yaml 2>&1 || :
  oc -n ${LOGGING_NS} get deployments -o yaml > $outdir/deployments.yaml 2>&1 || :
  oc -n ${LOGGING_NS} get replicasets -o yaml > $outdir/replicasets.yaml 2>&1 || :
  oc -n ${LOGGING_NS} get statefulsets -o yaml > $outdir/statefulsets.yaml 2>&1 || :
  oc -n ${LOGGING_NS} get pods -o yaml > $outdir/pods.yaml 2>&1 || :

  oc -n ${LOGGING_NS} get deployments -o wide > $outdir/deployments.txt 2>&1 || :
  oc -n ${LOGGING_NS} get replicasets -o wide > $outdir/replicasets.txt 2>&1 || :
  oc -n ${LOGGING_NS} get statefulsets -o wide > $outdir/statefulsets.txt 2>&1 || :
  oc -n ${LOGGING_NS} get pods -o wide > $outdir/pods.txt 2>&1 || :
  oc -n ${LOGGING_NS} get events -o wide > $outdir/events.txt 2>&1 || :
  oc -n ${LOGGING_NS} get secrets -o wide > $outdir/secrets.txt 2>&1 || :

  oc -n ${LOGGING_NS} extract secret/elasticsearch --to=$outdir ||:
  oc -n ${LOGGING_NS} extract configmap/fluentd --to=$outdir ||:

  oc -n ${LOGGING_NS} get elasticsearch -o yaml > $outdir/es.yaml ||:

  get_all_logging_pod_logs ${LOGGING_NS} $outdir
  get_all_olm_logs $outdir
  set -e
}

get_all_logging_pod_logs() {
  set +e
  local LOGGING_NS=$1
  local outdir=${2:-$ARTIFACT_DIR}
  local p
  local container
  oc -n ${LOGGING_NS} get pods -o wide > $outdir/pods.txt 2>&1

  for p in $(oc  -n ${LOGGING_NS} get pods -o jsonpath='{.items[*].metadata.name}') ; do
    oc -n ${LOGGING_NS} describe pod $p > $outdir/$p.describe 2>&1 || :
    oc -n ${LOGGING_NS} get pod $p -o yaml > $outdir/$p.yaml 2>&1 || :

    initContainers=$(oc -n ${LOGGING_NS} get po $p -o jsonpath='{.spec.initContainers[*].name}')
    for container in $initContainers ; do
        oc logs -n ${LOGGING_NS} -c $container $p > $outdir/$p.$container.init.log 2>&1
    done

    for container in $(oc -n ${LOGGING_NS} get po $p -o jsonpath='{.spec.containers[*].name}') ; do
      oc logs -n ${LOGGING_NS} -c $container $p > $outdir/$p.$container.log 2>&1
      case "$container" in
        elasticsearch*) oc -n ${LOGGING_NS} exec -c elasticsearch $p  -- logs > $outdir/$p.$container.exec.log 2>&1 ;;
        *) continue ;;
      esac
    done
  done
  set -e
}

get_all_olm_logs(){
    set +e
    local outdir=${1:-$ARTIFACT_DIR}
    local runtime=${2:-"120s"}
    oc  -n openshift-operator-lifecycle-manager logs --since=$runtime deployment/catalog-operator > $outdir/catalog-operator.logs 2>&1
    oc  -n openshift-operator-lifecycle-manager logs --since=$runtime deployment/olm-operator > $outdir/olm-operator.logs 2>&1
    oc  -n openshift-operator-lifecycle-manager get events > $outdir/olm-events.txt 2>&1
    set -e
}

wait_for_deployment_to_be_ready(){
  local namespace=$1
  local name=$2
  local timeout=$3
  os::cmd::try_until_text "oc -n $namespace get deployment $name -o jsonpath={.status.availableReplicas} --ignore-not-found" "1" $timeout
}

deploy_elasticsearch_operator() {
  # install the catalog containing the elasticsearch operator csv
  ${repo_dir}/olm_deploy/scripts/catalog-deploy.sh
  # install the elasticsearch operator from that catalog
  ${repo_dir}/olm_deploy/scripts/operator-install.sh
}
